#!/usr/bin/perl -w
use strict;
use warnings;
use Test::More tests => 29;

BEGIN { use_ok('Test::WWW::Mechanize::Catalyst', 'WIAB');
        use_ok 'WIAB::Controller::Content';
        use_ok 'FindBin';
        require "$FindBin::Bin/bin/make_test_env.pl";
        require "$FindBin::Bin/bin/make_tree.pl";
    }

END { require FindBin;
      require "$FindBin::Bin/bin/remove_test_env.pl"
   }

# TODO:  remove warnings from tests.

my $mech = Test::WWW::Mechanize::Catalyst->new;
$mech->get_ok('/', 'got home page');
$mech->content_contains('This is the home page', 'home page created');

# check links in  menu are rendered correctly
my @links;

# home
@links = $mech->find_all_links( url_regex => qr{content/$} );
$mech->links_ok( \@links, 'Check home page linked ok' );
# system("lynx -stdin <<EOF\n". $mech->uri . " " .  $mech->content . "\nEOF\n");
@links = $mech->find_all_links( text_regex => qr{\sHome} );
$mech->links_ok( \@links, 'home page link text correct' );

# one
@links = $mech->find_all_links( url_regex => qr{content/one/$} );
$mech->links_ok( \@links, '/content/one page linked ok' );
is (scalar(@links), 2, "two links /content/one on the page");
@links = $mech->find_all_links( text_regex => qr{\sone$} );
$mech->links_ok( \@links, '/content/one page link text correct' );
is (scalar(@links), 2, "two links to /content/one with correct text on the page");

# two_x
@links = $mech->find_all_links( url_regex => qr{content/two_x/$} );
$mech->links_ok( \@links, 'Check content/two_x linked ok' );
is (scalar(@links), 2, "two links content/two_x on the page");
@links = $mech->find_all_links( text_regex => qr{^two x$} );
is (scalar(@links), 2, "two links to /content/two_x with correct text on the page");
$mech->links_ok( \@links, '/content/two_x page link text correct' );

# three
@links = $mech->find_all_links( url_regex => qr{content/three/$} );
$mech->links_ok( \@links, 'Check /content/three page linked ok' );
is (scalar(@links), 2, "two links content/thee on this on the page");
@links = $mech->find_all_links( text_regex => qr{^three$} );
$mech->links_ok( \@links, '/content/three page link text correct' );
is (scalar(@links), 2, "two links to content/three on the page");


$mech->get_ok("http://localhost/content/one/", 'got content/one');

# one/two
@links = $mech->find_all_links( url_regex => qr{content/one/two/$} );
$mech->links_ok( \@links, 'Check one/two page linked ok' );
is (scalar(@links), 1, "one link to one/two on the page");
@links = $mech->find_all_links( text_regex => qr{\stwo$} );
$mech->links_ok( \@links, '/one/two page link text correct' );
is (scalar(@links), 1, "one link to one/two  with correct text on the page"); 

$mech->get_ok("http://localhost/content/one/two/", 'got content/one/two');

# one/two/three
@links = $mech->find_all_links( url_regex => qr{content/one/two/three/$} );
$mech->links_ok( \@links, 'Check one_two_three page linked ok' );
is (scalar(@links), 1, "one link to two/three to this on the page");
@links = $mech->find_all_links( text_regex => qr{^three$} );
$mech->links_ok( \@links, '/content/one/two/three page link text correct' );
is (scalar(@links), 1, "one link with correct text to one/two/three to this on the page");
